//
//  CSBuildPickerView.m
//  classroom
//
//  Created by Macx on 16/8/1.
//  Copyright © 2016年 LiuZhuo. All rights reserved.
//

#import "CSBuildPickerView.h"
#import "CSBuildManager.h"
#import "CSDateManager.h"
#define CSPickerHeight 216
@interface CSBuildPickerView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UIView *headView;
@property (strong, nonatomic) UIPickerView *pickView;
@property (strong,nonatomic) NSArray *firstAry;//一级数据源
@property (strong,nonatomic) NSArray *secondAry;//二级数据源
@property (nonatomic,assign) NSInteger firstCurrentIndex;//第一行当前位置
@property (nonatomic,assign) NSInteger secondCurrentIndex;//第二行当前位置
@end
@implementation CSBuildPickerView
- (instancetype)initWithFrame:(CGRect)frame {
    self =  [super initWithFrame:frame];
    if (self) {
        [self internalConfig];
    }
    return self;
}
//
//- (instancetype)initWithCoder:(NSCoder *)aDecoder {
//    self = [super initWithCoder:aDecoder];
//    if (self) {
//        [self internalConfig];
//    }
//    return self;
//}

- (void)internalConfig {
    _backView = [[UIView alloc] initWithFrame:self.frame];
    _backView.backgroundColor = [UIColor blackColor];
    _backView.alpha = 0.6;
    [self addSubview:_backView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [_backView addGestureRecognizer:tap];
    
//    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.pickView];
    
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, self.pickView.frame.origin.y - 43.5, self.frame.size.width, 43.5)];
    _headView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 0, 43.5, 43.5);
//    button.backgroundColor = [UIColor clearColor];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(cancleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:button];
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(self.frame.size.width - 42, 0, 32, 43.5);
//    button.backgroundColor = [UIColor clearColor];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(completionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:button];
    
    [self addSubview:_headView];
}

- (void)showPickerWithAreaName:(NSString *)ararName buildName:(NSString *)buildName{
    
    _firstAry = BuildingManager.areaDict;
    
    if (ararName.length > 0) {
        [_firstAry enumerateObjectsUsingBlock:^(NSDictionary *areaDict, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([areaDict[@"area"] isEqualToString:ararName]) {
                _firstCurrentIndex = idx+1;
                _secondAry = areaDict[@"build"];
            }
        }];
    } else {
        _firstCurrentIndex = 0;
        _secondCurrentIndex = 0;
    }
    
    [_secondAry enumerateObjectsUsingBlock:^(NSString *build, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([build isEqualToString:buildName]) {
            _secondCurrentIndex = idx+1;
        }
        
    }];
   
    [self.pickView reloadAllComponents];
    [self.pickView selectRow:_firstCurrentIndex inComponent:0 animated:NO];
    if (_secondAry.count > 0) {
        [self.pickView selectRow:_secondCurrentIndex inComponent:1 animated:NO];
    }
    
    [self show];
}
- (void)showPickerWithWeekName:(NSString *)weekName dayName:(NSString *)dayName{
    _firstAry = dataManager.weekDict;
    
    if (weekName.length > 0) {
        [_firstAry enumerateObjectsUsingBlock:^(NSDictionary *weekDict, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([weekDict[@"area"] isEqualToString:weekName]) {
                _firstCurrentIndex = idx+1;
                _secondAry = weekDict[@"build"];
            }
        }];
    } else {
        _firstCurrentIndex = 0;
        _secondCurrentIndex = 0;
    }
    
    [_secondAry enumerateObjectsUsingBlock:^(NSString *day, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([day isEqualToString:dayName]) {
            _secondCurrentIndex = idx+1;
        }
        
    }];
    
    [self.pickView reloadAllComponents];
    [self.pickView selectRow:_firstCurrentIndex inComponent:0 animated:NO];
    if (_secondAry.count > 0) {
        [self.pickView selectRow:_secondCurrentIndex inComponent:1 animated:NO];
    }
    
    [self show];

    
}
- (void)show {
    self.hidden = NO;
    _backView.alpha = 0.6;
    [UIView animateWithDuration:0.5 animations:^{
        self.pickView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - CSPickerHeight, self.frame.size.width, CSPickerHeight);
        _headView.frame = CGRectMake(0, self.pickView.frame.origin.y - 43.5, self.frame.size.width, 43.5);
    }];
}

- (void)hide {
    _backView.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        self.pickView.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height+44, self.frame.size.width, CSPickerHeight);
        _headView.frame = CGRectMake(0, self.pickView.frame.origin.y - 43.5, self.frame.size.width, 43.5);
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}

#pragma mark - UIPickerViewDataSource,UIPickerViewDelegate

//选项默认值
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated {
    return;
}

//设置列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    
    return 2;
}

//返回数组总数
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return _firstAry.count + 1;
    } else {
        return _secondAry.count + 1;
    }
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        if (row > 0) {
            if (row - 1 < _firstAry.count) {
                NSDictionary *data = _firstAry[row - 1];
                //获取校区
                NSString *str = data[@"area"];
                return str;
            }
        }
    } else {
        if (row > 0) {
            if (row - 1 < _secondAry.count) {
                //获取教学楼
                NSString *str = _secondAry[row - 1];
                return str;
            }
        }
    }
    return @"请选择";
}

//触发事件
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        _firstCurrentIndex = row;
        
        if (row > 0) {
            NSDictionary *dic = _firstAry[row - 1];
            _secondAry = dic[@"build"];
            _secondCurrentIndex = 0;
         
            
        } else {
            _secondAry = nil;
            _secondCurrentIndex = 0;
        }
        
        [self.pickView reloadAllComponents];
        if (_secondAry.count > 0) {
            [self.pickView selectRow:_secondCurrentIndex inComponent:1 animated:NO];
        }
    }  else {
        _secondCurrentIndex = row;
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:16]];
    }
    // Fill the label text here
    pickerLabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)completionButtonAction:(UIButton *)sender {
    
    NSString *areaName = @"";
    NSString *buildName = @"";
    
    if (_firstAry.count > 0) {
        if (_firstCurrentIndex > 0) {
            if (_firstCurrentIndex - 1 < _firstAry.count) {
                NSDictionary *data = _firstAry[_firstCurrentIndex - 1];
                //获取校区
                areaName = data[@"area"];
            }
        }
    }
    
    if (_secondAry.count > 0) {
        if (_secondCurrentIndex > 0) {
            if (_secondCurrentIndex - 1 < _secondAry.count) {
               
                //获取教学楼
                buildName = _secondAry[_secondCurrentIndex - 1];
            }
        }
    }
    
    
    if (_completion) {
        _completion(areaName,buildName);
    }
    if (_dateCompletion) {
        _dateCompletion(areaName,buildName);
    }
    [self hide];
}

- (void)cancleButtonAction:(UIButton *)sender {
    [self hide];
}

#pragma mark - 懒加载

- (UIPickerView*)pickView {
    if (!_pickView) {
        _pickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height + 44, self.frame.size.width, CSPickerHeight)];
        _pickView.delegate = self;
        _pickView.dataSource = self;
        _pickView.backgroundColor = [UIColor whiteColor];
//        [_pickView selectRow:0 inComponent:0 animated:NO];
    }
    return _pickView;
}

@end
